package com.app.jzgdirector.common.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.app.jzgdirector.R;


public class DialogUtil extends BaseDialog {

    public TextView tvMsg, tvMsg2;
    public LinearLayout lytBtn;
    private TextView tvTitle;
    private View line;

    public interface IBtnClickListener {
        void onBtnClick();
    }

    public DialogUtil(Context context) {
        super(context, R.style.dialog);
    }

    @Override
    public int getDialogLayout() {
        return R.layout.dialog_layout_one;
    }

    @Override
    public void init(Context context) {
        super.init(context);

        setCancelable(false);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);

        tvMsg = (TextView) findViewById(R.id.tvMsg);
        tvMsg2 = (TextView) findViewById(R.id.tvMsg2);
        line = findViewById(R.id.line);
        lytBtn = (LinearLayout) findViewById(R.id.lytBtn);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
    }

    /**
     * 设置弹出框信息(一个按钮“确定”)
     */
    public void setMsg(String msg) {
        setMsg(msg, "确定");
    }

    public void setMsg(String msg, IBtnClickListener listener) {
        tvMsg.setText(msg);
        lytBtn.removeAllViews();
        addButton("取消", new IBtnClickListener() {
            @Override
            public void onBtnClick() {
                dismiss();
            }
        });
        addButton("确定", listener);
    }

    /**
     * 设置弹出框信息(一个按钮)
     *
     * @param msg     弹出框信息
     * @param btnName 按钮文字
     */
    public void setMsg(String msg, String btnName) {
        tvMsg.setText(msg);
        lytBtn.removeAllViews();
        addButton(btnName, new IBtnClickListener() {
            @Override
            public void onBtnClick() {
                dismiss();
            }
        });
    }

    /**
     * 设置弹出框信息(一个按钮)
     *
     * @param msg     弹出框信息
     * @param btnName 按钮文字
     */
    public void setMsgWithButton(String msg, String btnName, IBtnClickListener listener) {
        tvMsg.setText(msg);
        lytBtn.removeAllViews();
        if (listener == null) {
            addButton(btnName, new IBtnClickListener() {
                @Override
                public void onBtnClick() {
                    dismiss();
                }
            });
        } else {
            addButton(btnName, listener);
        }
    }

    /**
     * 添加一个带有传入标题的设置
     *
     * @param title
     * @param msg
     * @param btnName
     */
    public void setMsg(String title, String msg, String btnName) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setTextColor(Color.BLACK);
        tvTitle.setText(title);
        tvMsg.setGravity(Gravity.LEFT);
        tvMsg.setTextSize(17);
        tvMsg.setTextColor(getContext().getResources().getColor(R.color.background));
        tvMsg.setText(msg);
        addButton(btnName, new IBtnClickListener() {
            @Override
            public void onBtnClick() {
                dismiss();
            }
        });
    }

    public void setTitleAndContent(String title, String msg) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setTextColor(Color.BLACK);
        tvTitle.setText(title);
        tvMsg.setGravity(Gravity.LEFT);
        tvMsg.setTextSize(17);
        tvMsg.setTextColor(getContext().getResources().getColor(R.color.background));
        tvMsg.setText(msg);
    }

    public void setText(String msg) {
        tvMsg.setText(msg);
    }

    public void setText(Spanned msg) {
        tvMsg.setText(msg);
    }

    public void setTowText(String msg, String msg2) {
        tvMsg2.setVisibility(View.VISIBLE);
        tvMsg.setText(msg);
        tvMsg2.setText(msg2);
    }

    public void setTitle(String msg) {
        tvTitle.setVisibility(View.VISIBLE);
        line.setVisibility(View.VISIBLE);
        tvTitle.setTextColor(Color.BLACK);
        tvTitle.setText(msg);
    }

    public void addButton(String btnName, IBtnClickListener listener, int color) {
        if (lytBtn.getChildCount() > 0) {
            LinearLayout lyt = new LinearLayout(getContext());
            LayoutParams lytParams = new LayoutParams((int) getContext().getResources().getDimension(R
                    .dimen.line), LayoutParams.MATCH_PARENT);
            lyt.setBackgroundColor(getContext().getResources().getColor(R.color.color_title));
            lyt.setLayoutParams(lytParams);
            lytBtn.addView(lyt);
        }

        TextView tv = new TextView(getContext());
        LayoutParams tvParams = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
        tv.setLayoutParams(tvParams);
        tv.setGravity(Gravity.CENTER);
        tv.setTypeface(Typeface.SANS_SERIF);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimension(R.dimen
                .dialog_btn_txt_size));
        int txtPadding = (int) getContext().getResources().getDimension(R.dimen.dialog_btn_padding);
        tv.setPadding(txtPadding, txtPadding, txtPadding, txtPadding);
        if (-1 == color) {
            tv.setTextColor(getContext().getResources().getColor(R.color.color_title));
        } else {
            tv.setTextColor(getContext().getResources().getColor(color));
        }
        tv.setText(btnName);
        final IBtnClickListener listenerTemp = listener;
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenerTemp != null) {
                    listenerTemp.onBtnClick();
                }
            }
        });
        tv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        ((TextView) v).setTypeface(Typeface.SANS_SERIF);
                        break;
                    case MotionEvent.ACTION_UP:
                        ((TextView) v).setTypeface(Typeface.SANS_SERIF);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        lytBtn.addView(tv);
    }

    public void addButton(String btnName, IBtnClickListener listener) {
        addButton(btnName, listener, -1);
    }

    //设置文字gravity
    public void setMsgGravent(int gravity) {
        tvMsg.setGravity(gravity);
    }

    //设置标题gravity
    public void setTitleGravent(int gravity) {
        tvTitle.setGravity(gravity);
    }

    public void setButtonColor(int index, int color) {
        TextView tv = (TextView) lytBtn.getChildAt(index);
        if (tv != null) {
            tv.setTextColor(color);
        }
    }

    public void setTvMsg(String msg) {
        tvMsg.setText(msg);
        lytBtn.removeAllViews();
    }
}
